﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    using System.Data;
    using Entities;
    public class KhachhangDAL: Base, Ibase
    {
        public int count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            var sql = "SELECT * FROM KHACH_HANG";
            return ExecuteQuery(sql);
        }

        public object Select(string id)
        {
            try
            {
                var sql = "SELECT * FROM KHACH_HANG WHERE MA_KH = '{0}'";
                sql = String.Format(sql, id);
                var tmp = ExecuteQuery(sql);

                if (tmp != null & tmp.Rows.Count > 0)
                {
                    var dg = new Khachhang();
                    dg.MA_KH = tmp.Rows[0]["MA_KH"] + "";
                    dg.HOTEN_KH = tmp.Rows[0]["HOTEN_KH"] + "";
                    dg.DIA_CHI = tmp.Rows[0]["DIA_CHI"] + "";
                    dg.SO_DT = tmp.Rows[0]["SO_DT"] + "";
                    dg.EMAIL = tmp.Rows[0]["EMAIL"] + "";


                    return dg;
                }
                else return null;
            }
            catch { return null; }
        }

        public int Update(object obj)
        {
            try
            {
                var o = (Khachhang)obj;
                var sql = "UPDATE KHACH_HANG SET HOTEN_KH=N'{1}', DIA_CHI=N'{2}', SO_DT= '{3}', EMAIL= '{4}' WHERE MA_KH = '{0}'";
                sql = string.Format(sql, o.MA_KH, o.HOTEN_KH, o.DIA_CHI, o.SO_DT, o.EMAIL);
                return ExecuteNonQuery(sql);
            }
            catch
            {
                return -1;
            }
        }

        public int Insert(object obj)
        {
            try
            {
                var o = (Khachhang)obj;
                var sql = "INSERT INTO KHACH_HANG(MA_KH, HOTEN_KH, DIA_CHI, SO_DT, EMAIL) VALUES ('{0}', N'{1}', N'{2}', N'{3}', N'{4}')";
                sql = string.Format(sql, o.MA_KH, o.HOTEN_KH, o.DIA_CHI, o.SO_DT, o.EMAIL);
                return ExecuteNonQuery(sql);
            }
            catch
            {
                return -1;
            }
        }

        public int Delete(string id)
        {
            try
            {

                var sql = "DELETE FROM KHACH_HANG WHERE MA_KH = '{0}'";
                sql = string.Format(sql, id);
                return ExecuteNonQuery(sql);
            }
            catch
            {
                return -1;
            }
        }
    }
}
